package com.sks.web.action.news;


import java.util.LinkedHashMap;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.htmlparser.Node;
import org.htmlparser.NodeFilter;
import org.htmlparser.Parser;
import org.htmlparser.filters.TagNameFilter;
import org.htmlparser.tags.ImageTag;
import org.htmlparser.util.NodeList;
import org.htmlparser.util.ParserException;
import org.springframework.stereotype.Controller;

import com.sks.bean.PageView;
import com.sks.bean.pojo.News;
import com.sks.service.news.NewsService;
import com.sks.web.action.BasicAction;
import com.sks.web.formbean.news.NewsForm;

/**
 * 主要功能为：前台资讯页面展现提供方法
 * @author yuanjiayuan
 *
 */

@Controller("/sks/news")
public class ClientNewsListAction extends BasicAction{

	@Resource(name="newsServiceBean")private NewsService newsService;
	
	private NewsBaseAction baseAction = new NewsBaseAction();
	/*------------------前段新闻列表展示---------------------*/
	/***
	 * 显示资讯首页新闻
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	public ActionForward indexNewsList(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) throws Exception {
		
		PageView<News> hotNews = baseAction.hotsNews(mapping, form,request,response,newsService);
		PageView<News> houseNews = baseAction.houseNews(mapping, form,request,response,newsService);
		PageView<News> policyNews = baseAction.policyNews(mapping, form,request,response,newsService);
		LinkedHashMap<String, String> order = new LinkedHashMap<String, String>();
		order.put("click", "desc");
		List<News> news = newsService.getScrollData(0,9999999,order).getResultlist();
		String str = null;
		String newsId = null;
		int num = 1;
		if(news.size()!= 0){
			do{		
				str = getImageURL(news.get(num).getContent());
				newsId = news.get(num).getNewsId().toString();
				num++;
			}while(str == null);
		}
		
		request.setAttribute("str", str);
		request.setAttribute("newsId", newsId);
		request.setAttribute("hotNews", hotNews);
		request.setAttribute("houseNews", houseNews);
		request.setAttribute("policyNews", policyNews);
		return mapping.findForward("indexNewsList");	
	}
	
	/**
	 * 用于提取新闻里的图片
	 * @param txt
	 * @return
	 * @throws ParserException
	 */
	private String getImageURL(String txt) throws  ParserException{
		  String img=null;
		  Parser parser;
		  NodeFilter filter=new TagNameFilter("img");
		  parser = Parser.createParser(txt,"GB2312");
		  NodeList nodelist=parser.parse(filter);
		  if(nodelist.size()!=0){
		   Node[] nodes=nodelist.toNodeArray();
		   for(int i=0;i<nodes.length;i++){
		    ImageTag image=(ImageTag) nodes[i];
		    if("".equals(img)){
		     img=image.getImageURL().toString();
		    }else{
		    	if(img==null){
		    		img = image.getImageURL().toString();
		    	}else {
		    		img=img+","+image.getImageURL().toString();
		    	}
		    }
		   }
		  }
		  return img;
		 }
	
	/**
	 * 显示市场房产动态新闻
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	public ActionForward houseNewsList(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) throws Exception {
		
		PageView<News> pageView = baseAction.houseNews(mapping, form,request,response,newsService);
		request.setAttribute("pageView", pageView);
		return mapping.findForward("houseNewsList");	
	}
	
	/***
	 * 显示热点新闻
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	public ActionForward policyNewsList(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) throws Exception {
		
		PageView<News> pageView = baseAction.policyNews(mapping, form,request,response,newsService);
		request.setAttribute("pageView", pageView);
		return mapping.findForward("policyNewsList");	
	}
	
	
	/***
	 * 显示热点新闻
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	public ActionForward hotNewsList(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) throws Exception {
		
		PageView<News> pageView = baseAction.hotsNews(mapping, form,request,response,newsService);
		request.setAttribute("pageView", pageView);
		PageView<News> lastNews = baseAction.lastNews(mapping,form,request,response,newsService);
        PageView<News> hotNews = baseAction.hotNews(mapping,form,request,response,newsService);
		request.setAttribute("lastNews", lastNews);
		request.setAttribute("hotNews", hotNews);
		return mapping.findForward("newsList");	
	}

	
	
	/***
	 * 显示新闻详细内容
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	
	public ActionForward infoList(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		baseAction.updateClick(mapping,form,request,response,newsService);
        NewsForm formbean = (NewsForm) form;
        PageView<News> lastNews = baseAction.lastNews(mapping,form,request,response,newsService);
        PageView<News> hotNews = baseAction.hotNews(mapping,form,request,response,newsService);
		News news = newsService.find(formbean.getNewsId());
		request.setAttribute("news", news);
		request.setAttribute("lastNews", lastNews);
		request.setAttribute("hotNews", hotNews);
		return mapping.findForward("info_list");
	}

	
}
